package com.mj.mafeed.adapter;

import java.util.List;

import com.mj.mafeed.R;
import com.mj.mafeed.activity.ReceiverDetailActivity;
import com.mj.mafeed.bean.ReceiverJSON;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 收货地址
 * @author zhaominglei
 * @date 2014-12-17
 * 
 */
public class ReceiverAdapter extends BaseAdapter {
	private List<ReceiverJSON> receivers;
	private Context context;
	
	public ReceiverAdapter(Context context, List<ReceiverJSON> receivers) {
		super();
		this.context = context;
		this.receivers = receivers;
	}

	public List<ReceiverJSON> getReceivers() {
		return receivers;
	}

	public void setReceivers(List<ReceiverJSON> receivers) {
		this.receivers.addAll(receivers);
	}


	@Override
	public int getCount() {
		return receivers.size();
	}

	@Override
	public Object getItem(int position) {
		return receivers.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		RelativeLayout relativeLayout;
		if (convertView != null) {
			relativeLayout = (RelativeLayout) convertView;
		} else {
			relativeLayout = (RelativeLayout) View.inflate(context,
					R.layout.receiver_item, null);
		}
		final ReceiverJSON receiverJSON = receivers.get(position);
		final Long receiverId = receiverJSON.getReceiverId();
		((TextView) relativeLayout.findViewById(R.id.receiver_item_template_areaname))
				.setText(receiverJSON.getAreaName());
		((TextView) relativeLayout.findViewById(R.id.receiver_item_template_id))
				.setText(String.valueOf(receiverId));
		((TextView) relativeLayout.findViewById(R.id.receiver_item_template_consignee))
				.setText(receiverJSON.getConsignee());
		((TextView) relativeLayout.findViewById(R.id.receiver_item_template_phone))
				.setText(receiverJSON.getPhone());
		((TextView) relativeLayout.findViewById(R.id.receiver_item_template_address))
				.setText(receiverJSON.getAddress());
		((TextView) relativeLayout.findViewById(R.id.receiver_item_template_zipCode))
				.setText(receiverJSON.getZipCode());
		ImageView eidtView = (ImageView) relativeLayout.findViewById(R.id.receiver_item_edit); //编辑
		eidtView.setTag(receiverId);// 通过标签确定单击时是否是该对象
		eidtView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Long descReceiverId = (Long) v.getTag();
				if (descReceiverId.equals(receiverId)) {
					Intent intent = new Intent(v.getContext(), ReceiverDetailActivity.class);
					Bundle bundle = new Bundle();
					bundle.putSerializable("receiverJSON", receiverJSON);
					intent.putExtras(bundle);
					intent.putExtra("referer", "ReceiverListActivity");
					intent.putExtra("action", "edit");
					v.getContext().startActivity(intent);
					((Activity)v.getContext()).finish();
				}
			}
		});
		
		return relativeLayout;
	}

}
